﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using MySql.Data.MySqlClient;
using System.Data.Odbc;

namespace LectorHuellaC
{
    class cls_productos
    {
        log4net.ILog logger;
        ConectaBD conBD = new ConectaBD();

        OdbcCommand cm;
        OdbcParameter prm;
        OdbcDataReader dr;

        public cls_productos()
        {
            log4net.Config.XmlConfigurator.Configure();
            logger = log4net.LogManager.GetLogger(typeof(Program));
            
        }


        public DataTable getProductInfo(int product_id,int category_id)
        {
         try
                {
                    string product_info = "call getAllProductosInfo()";
                    conBD.ConectaODBC();
                    cm = new OdbcCommand();
                    cm.Connection = conBD.ConexionODBC;
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.CommandText = product_info;

                    //cm.Parameters.AddWithValue("producto_id", product_id);
                    //cm.Parameters.AddWithValue("category_id", product_id);
                    //OdbcDataReader reader = cm.ExecuteReader();
                    //DataSet ds = new DataSet();
                    DataTable table = new DataTable();
                    OdbcDataAdapter da = new OdbcDataAdapter(cm);
                    da.Fill(table);
                    return table;
                }
            catch(Exception ex){
                logger.Error("" + ex.Message);
                return null;
            }
        }
        
        public bool insertarActualizarProductos(int id,string name,int categoy_id,int product_code,string description)
        {
            string insertar_actualizar = "";
            if (id == 0)
            {
                insertar_actualizar = "call InsertarProducto (?,?,?,?)";
                
            }
            else
            {
                insertar_actualizar = "call actualizarProducto (?,?,?,?,?) ";
            }

            //Use ODBC call syntax.
            conBD.ConectaODBC();
            cm = new OdbcCommand();
            cm.Connection = conBD.ConexionODBC;
            cm.CommandType = CommandType.StoredProcedure;
            cm.CommandText = insertar_actualizar;
            if (id != 0)
            {
                cm.Parameters.AddWithValue("_product_id", id);
            }
            cm.Parameters.AddWithValue("product_name", name );
            cm.Parameters.AddWithValue("category_id", categoy_id );
            cm.Parameters.AddWithValue("product_code", product_code);
            cm.Parameters.AddWithValue("product_description",description);
            int rowsaffected=cm.ExecuteNonQuery();

            if (rowsaffected == 0)
            {
                return false;
                }
            else
            {
                return true;  
            }
        }

        public bool insertarActualizarCategorias() 
        {
            return true;
        }

    }
}
